---
title: Using opaque types
description: Creating and using opaque keys
keywords:
  - electrodb
  - docs
  - concepts
  - dynamodb
  - query
  - entity
  - attribute
  - schema
  - index
layout: ../../../layouts/MainLayout.astro
---

## Opaque Keys/Ids

If you use Opaque Keys for identifiers or other primitive types, you can use the function `CustomAttributeType` and pass it the primitive base type of your key ('string', 'number', 'boolean'). This can be useful to gain more precise control over which properties can be used as entity identifiers, create unique unit types, etc.

```typescript
import { Entity, CustomAttributeType } from "electrodb";

const UniqueKeySymbol: unique symbol = Symbol();
type EmployeeID = string & { [UniqueKeySymbol]: any };

const UniqueAgeSymbol: unique symbol = Symbol();
type Month = number & { [UniqueAgeSymbol]: any };

const table = "workplace_table";

const person = new Entity(
  {
    model: {
      entity: "personnel",
      service: "workplace",
      version: "1",
    },
    attributes: {
      employeeId: {
        type: CustomAttributeType<EmployeeID>("string"),
      },
      firstName: {
        type: "string",
        required: true,
      },
      lastName: {
        type: "string",
        required: true,
      },
      ageInMonths: {
        type: CustomAttributeType<Month>("number"),
      },
    },
    indexes: {
      record: {
        pk: {
          field: "pk",
          composite: ["employeeId"],
        },
        sk: {
          field: "sk",
          composite: [],
        },
      },
    },
  },
  { table },
);
```

[![Try it out!](https://img.shields.io/badge/electrodb-try_out_this_example_›-%23f9bd00?style=for-the-badge&logo=amazondynamodb&labelColor=1a212a)](https://electrodb.fun/?ssl=3&ssc=29&pln=37&pc=2#code/JYWwDg9gTgLgBAbzgUQHY2DAngGjgYQFcBnGCEAQRhimACNCYBTAFSzCbgF84AzKcnADkTADZMAxjQgATOkIDcAKCUSIqUnACqqYAEdCTANJMsAZSwg6EUQC44hXQc7FL10XAC8cC1ZsAKAEplbA4UcFEILCYmAEkAES84UlpUAHM4ADJEAG0dfUMTczcbAF17AENULC5lVXVNfOcKNKZfd3tHApcSj292gOClUM4AWXUYAAsk1EIrJigs3KbDFrbe8rgqmrqRuAAFBeJ1VDEAJRtObwQlODu4EfsRCKiYxVv70grYeIrmexSwHSynucCYqBkv2YAH4ATQ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